#pragma once

#include "logger.hpp"
#include <mysql/mysql.h>
#include <iostream>
#include <string>
#include <cstring>
#include <errno.h>
#include <jsoncpp/json/json.h>
#include <memory>
#include <sstream>
#include <vector>
#include <fstream>

class mysql_util
{
public:
    // 创建以及初始化数据库
    static MYSQL *mysql_create(
        const std::string &host,
        const std::string &user,
        const std::string &passwd,
        const std::string &dbname,
        const uint16_t port = 3306)
    {
        // 创建句柄
        MYSQL *mysql = mysql_init(NULL);
        if (mysql == NULL)
        {
            ELOG("mysql_init fail!");
            return NULL;
        }
        // 连接mysql服务器
        MYSQL *my_sql = mysql_real_connect(mysql, host.c_str(), user.c_str(), passwd.c_str(), dbname.c_str(), port, NULL, 0);
        if (my_sql == NULL)
        {
            ELOG("mysql_create fail:%s\n", mysql_error(mysql));
            mysql_close(mysql);
        }

        // 设置字符串
        if (mysql_set_character_set(mysql, "utf8") != 0)
        {
            ELOG("mysql_set_character_set fail:%s\n", mysql_error(mysql));
            mysql_close(mysql);
        }

        return mysql;
    }

    // 执行sql语句
    static bool mysql_exec(MYSQL *mysql, const char *sql)
    {
        if (mysql_query(mysql, sql) != 0)
        {
            ELOG("%s\n", sql);
            ELOG("mysql_exec fail:%s\n", mysql_error(mysql));
            return false;
        }

        return true;
    }

    // 关闭句柄
    static void mysql_destroy(MYSQL *mysql)
    {
        if (mysql != NULL)
        {
            mysql_close(mysql);
        }
        return;
    }
};

class json_util
{
public:
    // 将Json对象，序列化成字符串
    static bool serialize(const Json::Value &values, std::string &str)
    {
        Json::StreamWriterBuilder swb;
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        std::stringstream ss;
        int ret = sw->write(values, &ss);
        if (ret != 0)
        {
            ELOG("serialize fail");
            return false;
        }
        str = ss.str();
        return true;
    }

    // 将字符串，反序列化形成Json对象
    static bool deserialize(std::string &str, Json::Value &values)
    {
        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
        std::string err;
        // begin end Json err
        bool ret = cr->parse(str.c_str(), str.c_str() + str.size(), &values, &err);
        if (ret == false)
        {
            ELOG("deserialize fail");
            return false;
        }

        return true;
    }
};

class string_util
{
public:
    static int splict(const std::string &str, const std::string &step, std::vector<std::string> &vs)
    {
        // hello,nhao,,,world
        size_t pos, index = 0; // pos位置和当前位置
        while (index < str.size())
        {
            pos = str.find(step, index); // 找什么，从哪个位置开始找
            if (pos == std::string::npos)
            {
                vs.push_back(str.substr(index));
                break;
            }
            // 遇到多个分隔符
            if (pos == index)
            {
                index = index + step.size();
                continue;
            }

            vs.push_back(str.substr(index, (pos - index)));
            index = pos + step.size();
        }
    }
};

class file_util
{
public:
    static bool read(const std::string &filename, std::string &body)
    {
        // 打开文件
        std::ifstream ifs(filename, std::ios::binary);
        if (ifs.is_open() == false)
        {
            ELOG("%s file open failed!!", filename.c_str());
            return false;
        }
        // 获取文件大小
        size_t fsize = 0;
        ifs.seekg(0, std::ios::end);
        fsize = ifs.tellg();
        ifs.seekg(0, std::ios::beg);
        body.resize(fsize);
        // 将文件所有数据读取出来
        ifs.read(&body[0], fsize);
        if (ifs.good() == false)
        {
            ELOG("read %s file content failed!", filename.c_str());
            ifs.close();
            return false;
        }
        // 关闭文件
        ifs.close();
        return true;
    }
};
